﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using YUCodeGen.ProductModel;

namespace YUCodeGen.Controllers
{
    public class AngularController : Controller
    {
        // GET: Angular
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Template(string id)
        {
            Type typeModel = typeof(Student);
            ViewData["ClassName"] = typeModel.Name;
            ViewData["cat"] = id;
            PropertyInfo[] props = typeModel.GetProperties();
            return View(props);
        }


        //Render Angular CRUD Compnent by a csharp model.
        public ActionResult TSComp(string id)
        {
            string className = "Student";
            ViewData["ClassName"] = className;
            ViewBag.Id = id;
            Type typeModel = typeof(Student);
            PropertyInfo[] props = typeModel.GetProperties();
            return View(props);
        }


        //Render a view to string;
        public string Save()
        {
            Type typeModel = typeof(Student);
            string className = typeModel.Name;
            string path = @"e:\tmp\out";
            ViewData["ClassName"] = className;
            SaveACat(path, typeModel, "curd");
            SaveCreate(path, typeModel);
            SaveDetail(path, typeModel);
            SaveACat(path, typeModel, "list");
            return $"Done:{typeModel.Name}"; 
        }

        private void SaveDetail(string path, Type typeModel)
        {
            SaveCreateDetail(path, typeModel, "detail");
        }

        private void SaveCreate(string path, Type typeModel)
        {
            SaveCreateDetail(path, typeModel, "create");
        }

        private void SaveCreateDetail(string path, Type typeModel,string cat)
        {
            string className = typeModel.Name;
            getFileNames(path,className,cat,out string fntemplate,out string fncomp,out string fncss);
            
            ViewData["cat"] = cat;
            PropertyInfo[] props = typeModel.GetProperties();
            string s1 = RenderRazorViewToString("Template", props);
            System.IO.File.WriteAllText(fntemplate, s1);
            string s2 = RenderRazorViewToString("TSComp", props);
            System.IO.File.WriteAllText(fncomp, s2);
            System.IO.File.WriteAllText(fncss, "");
            
        }

        private void getFileNames(string path, string className, string cat, out string fntemplate, out string fncomp, out string fncss)
        {
             fntemplate = $@"{path}\{className}-{cat}.component.html";
             fncomp = $@"{path}\{className}-{cat}.component.ts";
             fncss = $@"{path}\{className}-{cat}.component.css";
        }

        private void SaveACat(string path, Type classType,string cat)
        {
            ViewData["cat"] = cat;
            string className = classType.Name;
            getFileNames(path, className, cat, out string fntemplate, out string fncomp, out string fncss);
            string stext = "";
            stext = RenderRazorViewToString($"{cat}Template", classType.GetProperties());
            System.IO.File.WriteAllText(fntemplate, stext);
            stext = RenderRazorViewToString($"{cat}Component", classType.GetProperties());
            System.IO.File.WriteAllText(fncomp, stext);
            System.IO.File.WriteAllText(fncss, "");
        }



        public string RenderRazorViewToString(string viewName, object model)
        {
            ViewData.Model = model;
            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);
                return sw.GetStringBuilder().ToString();
            }
        }
    }
}